package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.central.enums.OperationStationProcessingStatusEnum;
import com.ruoyi.central.enums.OperationTypeEnum;
import com.ruoyi.central.service.ITopCentralAreaService;
import com.ruoyi.common.core.domain.Select;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.bean.EnumUtils;
import com.ruoyi.common.utils.service.ServiceUtils;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralOperationStation;
import com.ruoyi.central.service.ITopCentralOperationStationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
import java.util.function.Function;

/**
 * 操作工位Controller
 * 
 * @author ruoyi
 * @date 2022-03-02
 */
@Controller
@RequestMapping("/central/centralOperationStation")
public class TopCentralOperationStationController extends BaseController
{
    private String prefix = "central/centralOperationStation";

    @Autowired
    private ITopCentralOperationStationService topCentralOperationStationService;

    @Autowired
    private ITopCentralAreaService areaService;

    @RequiresPermissions("central:centralOperationStation:view")
    @GetMapping()
    public String centralOperationStation()
    {
        return prefix + "/centralOperationStation";
    }

    /**
     * 获取自动操作工位
     * @return
     */
    @GetMapping("/getAutoOperationStationList")
    @ResponseBody
    public AjaxResult getAutoOperationStationList(){

        List<TopCentralOperationStation> list = topCentralOperationStationService.getAutoOperationStationList();

        Function<TopCentralOperationStation, Select> selectFunction = operationStation -> {
            Select select = new Select(operationStation.getId(), operationStation.getName());
            select.setData(operationStation);
            select.setDisable(EnumUtils.equals(OperationStationProcessingStatusEnum.PROCESSING,operationStation.getProcessingStatus()));
            return select;
        };

        List<Select> selectsForAutoOperationStation = SelectUtils.buildSelectList(list,selectFunction);

        return AjaxResult.success(selectsForAutoOperationStation);
    }
    /**
     * 查询操作工位列表
     */
    @RequiresPermissions("central:centralOperationStation:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralOperationStation topCentralOperationStation)
    {
        startPage();
        List<TopCentralOperationStation> list = topCentralOperationStationService.selectList(topCentralOperationStation);

        ServiceUtils.complete(list,areaService,(areaMap,item)->{
            item.setArea(areaMap.get(item.getAreaId()));
        });

        return getDataTable(list);
    }

    /**
     * 查询简单的操作工位列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralOperationStation topCentralOperationStation){

        Collection<TopCentralOperationStation> topCentralOperationStationList = topCentralOperationStationService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralOperationStationList,TopCentralOperationStation::getId,TopCentralOperationStation::getName));
    }

    /**
     * 获取操作工位详细信息
     */
    @RequiresPermissions("central:centralOperationStation:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralOperationStationService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralOperationStation 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralOperationStation topCentralOperationStation){
        int weigth = topCentralOperationStationService.getMaxOfWeight(topCentralOperationStation);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载操作工位导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralOperationStation> util = new ExcelUtil<>(TopCentralOperationStation.class);
        util.importTemplateExcel(response, "操作工位数据");
    }

    /**
     * 导入操作工位数据
     * @param file 操作工位数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "操作工位", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralOperationStation:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralOperationStation> util = new ExcelUtil<>(TopCentralOperationStation.class);
        List<TopCentralOperationStation> topCentralOperationStationList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralOperationStationService.updateBatchById(topCentralOperationStationList)){
                return AjaxResult.success("导入操作工位数据覆盖成功！");
            }
            return AjaxResult.error("导入操作工位数据覆盖失败！");
        }
        if(topCentralOperationStationService.insertBatch(topCentralOperationStationList)){
            return AjaxResult.success("导入操作工位数据新增成功！");
        }
        return AjaxResult.error("导入操作工位数据新增失败！");
    }

    /**
     * 导出操作工位列表
     */
    @RequiresPermissions("central:centralOperationStation:export")
    @Log(title = "操作工位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralOperationStation topCentralOperationStation)
    {
        List<TopCentralOperationStation> list = topCentralOperationStationService.selectList(topCentralOperationStation);
        ExcelUtil<TopCentralOperationStation> util = new ExcelUtil<TopCentralOperationStation>(TopCentralOperationStation.class);
        return util.exportExcel(list, "操作工位数据");
    }

    /**
     * 新增操作工位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存操作工位
     */
    @RequiresPermissions("central:centralOperationStation:add")
    @Log(title = "操作工位", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralOperationStation topCentralOperationStation)
    {
        return toAjax(topCentralOperationStationService.insert(topCentralOperationStation));
    }

    /**
     * 修改操作工位
     */
    @RequiresPermissions("central:centralOperationStation:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralOperationStation topCentralOperationStation = topCentralOperationStationService.selectById(id);
        mmap.put("topCentralOperationStation", topCentralOperationStation);
        return prefix + "/edit";
    }

    /**
     * 修改保存操作工位
     */
    @RequiresPermissions("central:centralOperationStation:edit")
    @Log(title = "操作工位", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralOperationStation topCentralOperationStation)
    {
        return toAjax(topCentralOperationStationService.updateById(topCentralOperationStation));
    }

    /**
     * 修改操作工位 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralOperationStation:edit")
    @Log(title = "操作工位", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralOperationStationService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 删除操作工位
     */
    @RequiresPermissions("central:centralOperationStation:remove")
    @Log(title = "操作工位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralOperationStationService.deleteByIds(ids));
    }
}
